<?php
namespace jy_hehuofen;
defined('BY_JYA') or exit('error');
/*
	分销插件工具类
*/
class model extends \plugin{
	
	public function checklogin(){
		cfc('core')->checklogin();
		$this->model('member')->register();
	}
	public function current_level_up_types(){
		return str_array($this->setting['level_up_types']);
	}
	public function available_areas(){
		$areas=file_get_contents(ROOT_D.'/core/resource/area_town.txt');
		$areas=unserialize($areas);
		return $areas;
	}
	public function get_area_code($area){
		$address=$area['province'];
		if($area['city']){
			$address.=' '.$area['city'];
		}
		if($area['district']){
			$address.=' '.$area['district'];
		}
		if($area['street']){
			$address.=' '.$area['street'];
		}
		$area_code=pdo_getcolumn('core_area',array('address'=>$address),'code');
		if(!$area_code){
			$isup=pdo_count('core_area',array(),'code');
			if($isup==0){
				require ROOT_D.'/update/update_area.php';
			}
		}
		return $area_code;
	}
	public function add_upfee(){
		$params=array('uniacid'=>$_SESSION['uniacid']);
		$parameter=array(
			'params'=>$params,
			'limit'=>'1',
			'orderby'=>'id desc',
		);
		$list=$this->get_list('upfee',$parameter,$total);
		$params['ispay']=1;
		if(count($list)>0){
			$start_time=$list[0]['end_time'];
		}else{
			$parameter['orderby']='id asc';
			$pindex = max(1, intval($_RQ['page']));
			$list=pdo_getslice('jy_weishop_order',$params,array($pindex,1),$total,'','',$parameter['orderby']);
			if(count($list)>0){
				$start_time=$list[0]['createtime'];
			}else{
				return false;
			}
		}
		//获取周期；
		$up_days=$this->setting['up_days']?:7;
		$up_days=intval($up_days)?:7;
		$end_time=($start_time+$up_days*24*3600)-1;	
		if($end_time>TIMESTAMP){
			return false;
		}
		$params['createtime between']=array($start_time,$end_time);
		$ordersfee=pdo_getsum('jy_weishop_order',$params,'price');
		$params=array('status'=>1,'uniacid'=>$_SESSION['uniacid'],'check_time <='=>$end_time);
		$teams=$this->getall('team',$params);
		$alluser_num=count($teams);
		$level_fee=0;
		foreach($teams as $item){
				$user_num=$this->count('team',array('level_id'=>$item['level_id'],'status'=>1,'uniacid'=>$_SESSION['uniacid'],'check_time <='=>$end_time));
				$level=$this->get('level',array('id'=>$item['level_id'],'uniacid'=>$_SESSION['uniacid']),array('fee_per','max_redfee'));
				$fee_per=$level['fee_per'];
				$max_redfee=$level['max_redfee'];
				if($max_redfee && $item['allredfee']>=$max_redfee){
					continue;
				}
				if($user_num && $fee_per && $ordersfee){
					$money=$fee_per*$ordersfee/$user_num;
				}
				if($money>0){
					$level_fee +=$money;
					$remark=array(
						'allfee'=>$ordersfee,
						'fee_per'=>$fee_per,
						'user_num'=>$user_num,
					);
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$item['mid'],
						'level'=>$item['level_id'],
						'end_time'=>$end_time,
						'remark'=>ijson_encode($remark),
						'money'=>$money,
						'createtime'=>TIMESTAMP,
						'status'=>0,
						'type'=>0,
					);
					$this->insert('commission',$new_commission);
					$this->update('team',array('allredfee +='=>$money),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
				}
		}
		$fee=array(
				'uniacid'=>$_SESSION['uniacid'],
				'user_num'=>$alluser_num,
				'level_fee'=>$level_fee,
				'fee'=>$ordersfee,
				'status'=>0,
				'start_time'=>$start_time,
				'end_time'=>$end_time,
		);
		$this->insert('upfee',$fee);
		$this->add_upfee();
		return true;
	}
}